package com.aaa.sso.shiro;

import com.aaa.common.entity.User;
import com.aaa.sso.service.RemoteUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;

/**
 * @ FileName: UserRealm
 * @ Description:
 * @ Author:  STB
 * @ CreateTime: 2025/9/3 0003 19:12
 * @ Version: 1.0.0
 */
public class UserRealm extends AuthorizingRealm {
    @Resource
    private RemoteUserService remoteUserService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String  userName = (String) authenticationToken.getPrincipal();
        User user = remoteUserService.queryByUserName(userName);
        if (user==null){
            throw new AccountException();
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),this.getName());
    }
    @Override
    public String getName() {
        return "UserRealm";
    }
}
